'''
Created on Feb 28, 2011

@author: Chris
'''
import string
import Feature
import Suggestion
import FeatureSuggestions
import en
import nltk_contrib

class AvgWordLength(object):
	'''
	of a feature that will implement FeatureSuggestions. This class will contain a list of suggestions corresponding with the feature type. 
	Each of these classes (taken from the feature list from this design document) 
	will have a different algorithm associated with it (also outlined in this design document)
	'''


	def __init__(self, featureHandler, feature, avgWordLength):	
		self.wordFrequencies = {}
		self.wordReplacements = {}
		#self.wordCountSuggestions = []
		self.featureSuggestions = []

		for section in range(len(featureHandler.documentSections)):
			documentSentences = featureHandler.documentSections[section].getSentences()
		 
			for sentence in range(len(documentSentences)):
				wordsInSentence = documentSentences[sentence].getWords()
				tempSentence = ''
				
				for word in range(len(wordsInSentence)):
					'''See of the word exists in the dictionary'''
					'''print 'Word'''
					tempSentence+= self.addCountedWord(word, wordsInSentence)

				
				taggedSentence = en.sentence.tag(tempSentence)
				'''print taggedSentence'''
			 
				for i in range(len(taggedSentence)):
					
					'''print 'Tagged Word'''
					tempString = str(taggedSentence[i][0])
					tempString = self.cleanString(tempString);

					'''print tempString'
					
					print 'Tagged POS'
					print taggedSentence[i][1]'''
					tempCheck = taggedSentence[i][1]
					self.getSynonyms(tempCheck, tempString, taggedSentence)
					

				   
		wordCount = float(avgWordLength)
		wordCount = round(wordCount)
		top1Percent = wordCount * 0.01

		if top1Percent < 1:
			top1Percent = 1

		#print '==================================================='
		#print top1Percent
		'''print top1Percent'''
		
		topKeys = sorted(self.wordFrequencies, key=self.wordFrequencies.get)
		#If document word length > corpus word length, must add more long words
		#Else make shorter
		
		makeLonger = False
		if feature.value > avgWordLength:
			topKeys.reverse()
			makeLonger = True
		#Otherwise, we do the least
		#print topKeys

		acceptedSuggestions = 0
		#print self.wordReplacements
		while acceptedSuggestions < int(top1Percent):
			for j in range(0, len(topKeys)):  
				if not acceptedSuggestions < int(top1Percent):
					break
				'''print 'Top Word'
				print topKeys[j]
				print 'Replacement Word'''
				'''Gotta figure out how the return value works for hypernyms'''
				'''print self.wordReplacements[topKeys[j]][1][0]'''
				'''Create a new suggestion with self.wordFrequencies[topKeys[j]]'''
				'''Replacement text in Suggestion will be self.wordFrequencies[topKeys[j]]'''
				if topKeys[j] != '':
					try:
						if len(self.wordReplacements[topKeys[j]]) < 1:
							continue
						for x in range(0, len(self.wordReplacements[topKeys[j]][0])):
							if makeLonger:
								if len(self.wordReplacements[topKeys[j]][0][x]) >= topKeys[j]:
									featureSuggestion = FeatureSuggestions.FeatureSuggestions(feature)
									self.wordCountSuggestions = Suggestion.Suggestion('Word length change', featureHandler.document, topKeys[j], self.wordReplacements[topKeys[j]][0][x], '#0000A0')
									featureSuggestion.addSuggestions(self.wordCountSuggestions)
									#return featureSuggestion
									self.featureSuggestions.append(featureSuggestion)
									acceptedSuggestions = acceptedSuggestions + 1
									break
							else:
								if len(self.wordReplacements[topKeys[j]][0][x]) < topKeys[j]:
									featureSuggestion = FeatureSuggestions.FeatureSuggestions(feature)
									self.wordCountSuggestions = Suggestion.Suggestion('Word length change', featureHandler.document, topKeys[j], self.wordReplacements[topKeys[j]][0][x], '#0000A0')
									featureSuggestion.addSuggestions(self.wordCountSuggestions)
									#return featureSuggestion
									self.featureSuggestions.append(featureSuggestion)
									acceptedSuggestions = acceptedSuggestions + 1
									break
					except KeyError:
						continue
			break
					
	def getFeatureSuggestions(self):
		return self.featureSuggestions
			
	def addCountedWord(self, word, wordsInSentence):
		tempString = string.lower(wordsInSentence[word])
		tempString = tempString.replace(',', '')
		tempString = tempString.replace('"', '')
		'''print tempString'''
		if tempString in self.wordFrequencies:
			tempWordValue = self.wordFrequencies[tempString]
			newTempWordValue = tempWordValue + 1
			self.wordFrequencies[tempString] = newTempWordValue
		else:
			self.wordFrequencies[tempString] = 1
	
		return wordsInSentence[word] + ' '
	
	def cleanString(self, tempString):
		tempString = string.lower(tempString)
		tempString = tempString.replace(',', '')
		tempString = tempString.replace('"', '')
		return tempString
		
	def getSynonyms(self, tempCheck, tempString, taggedSentence):
		if tempString != "":
			if((tempCheck == "NN") or (tempCheck == "NNS")):
				if tempCheck == "NNS":
					self.wordReplacements[tempString] = en.noun.hypernyms(en.noun.singular(tempString))
				else:
					self.wordReplacements[tempString] = en.noun.hypernyms(tempString)
		 
			elif ((tempCheck == "JJ") or (tempCheck == "JJR") or (tempCheck == "JJS")):
				self.wordReplacements[tempString] = en.adjective.hypernyms(tempString)
		 
			elif ((tempCheck == "RB") or (tempCheck == "RBR") or (tempCheck == "RBS")):
				self.wordReplacements[tempString] = en.adverb.hypernyms(tempString)
		
			else:
				if tempString in self.wordFrequencies:
			#		'''print 'Delete this word'
			#		print self.wordFrequencies[tempString]'''
					del self.wordFrequencies[tempString]
			#		'''Sort the word frequencies dictionary'''